package com.aca.market.controller;

import com.aca.market.entity.Catalog;
import com.aca.market.entity.Item;
import com.aca.market.entity.TypeItem;
import com.aca.market.service.ICatalogService;
import com.aca.market.service.IItemService;
import com.aca.market.service.ITypeItemService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class CatalogController {

    @Autowired
    ICatalogService catalogService;
    @Autowired
    IItemService itemService;
    @Autowired
    ITypeItemService typeItemService;

    //后

    @GetMapping("/catalog/list")
    public String getCatalogs(Model model){
        List<Catalog> catalogs = catalogService.list();
        model.addAttribute("catalog",catalogs);
        return "catalog-list";
    }

    @GetMapping("/catalog/toAdd")
    public String toAddCatalog(Model model){
        model.addAttribute("types",catalogService.getAllIdNameMap());
        return "catalog-add";
    }

    @PostMapping("/catalog/add")
    public String addCatalog(Catalog catalog){
        Catalog tmp = catalog;
        if (!tmp.getTypeName().isEmpty()){
            tmp.setUpdateTime(LocalDateTime.now());
            catalogService.save(tmp);
            return "redirect:/catalog/list";
        }
        return "fail";
    }

    @GetMapping("/catalog/detail")
    public String getCatalog(@RequestParam Long typeId, Model model){
        Catalog catalog = catalogService.getById(typeId);
        model.addAttribute("catalog",catalog);
        model.addAttribute("types",catalogService.getAllIdNameMap());
        return "catalog-detail";
    }

    @PostMapping("/catalog/update")
    public String updateCatalog(Catalog catalog){
        if (!catalog.getTypeName().isEmpty()){
            Catalog tmp = catalog;
            tmp.setUpdateTime(LocalDateTime.now());
            catalogService.updateById(tmp);
            return "redirect:/catalog/list";
        }
        return "fail";
    }

    @GetMapping("/catalog/del")
    public String delCatalog(@RequestParam Long typeId){
        itemService.update(new UpdateWrapper<Item>()
                .eq("type",typeId)
                .set("updated_time",LocalDateTime.now())
                .set("type_id",null));
        typeItemService.update(new UpdateWrapper<TypeItem>()
                .eq("type_id",typeId)
                .set("type_id",null));
        return catalogService.removeById(typeId)?"redirect:/catalog/list":"fail";
    }

    @PostMapping("/catalog/delete/{typeIds}")
    public String delCatalogs(@PathVariable List<Long> typeIds){
        itemService.update(new UpdateWrapper<Item>()
                .in("type",typeIds)
                .set("updated_time",LocalDateTime.now())
                .set("type_id",null));
        return catalogService.removeByIds(typeIds)?"redirect:/catalog/list":"fail";
    }


}
